<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <!-- <template #toolbar>
        <a-button type="primary" @click="handleForm({})" v-auth="'drill:fireDrill:edit'">
          <Icon icon="i-fluent:add-12-filled" /> {{ t('新增') }}
        </a-button>
      </template> -->
      <template #firstColumn="{ record }">
        <a @click="handleForm({ tid: record.tid })">
          {{ record.enterpriseId }}
        </a>
      </template>
      <!-- 新增 action 插槽 -->
      <template #action="{ record }">
        <a-button @click="handleForm({ tid: record.tid })" type="text">
          <span style="font-size: 14px; color: #4169E1;">演练详情</span>
        </a-button>
        <!-- <a-button
          v-auth="'drill:fireDrill:edit'"
          type="text"
          style="color: green;"
          @click="handleEndFireDrill(record)"
          :disabled="record.fireExecutionStatus === '已结束'"
        >
          <span style="font-size: 12px;">{{ t('火情演练结束') }}</span>
        </a-button> -->
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts" setup name="ViewsDrillFireDrillList">
  import { unref, ref } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { fireDrillDelete, fireDrillListData, fireDrillEnded } from '/@/api/drill/fireDrill';
  import { unitEnterpriseDominantListData } from '/@/api/unitmanage/unitEnterpriseDominant';
  import { useDrawer } from '/@/components/Drawer';
  import { officeTreeData } from '/@/api/sys/office';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import { Popconfirm } from 'ant-design-vue'; // 引入 Popconfirm 组件

  const { t } = useI18n('drill.fireDrill');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);

  const getTitle = {
    icon: meta.icon || 'i-ant-design:book-outlined',
    value: meta.title || t('火情演练管理'),
  };

  const EnterpriseParams = ref<Recordable>({
    name: ''
  });

  const searchForm: FormProps = {
    baseColProps: { md: 8, lg: 6 },
    labelWidth: 90,
    schemas: [
      // {
      //   label: t('创建时间'),
      //   field: 'createTime',
      //   component: 'RangePicker',
      //   componentProps: {
      //     format: 'YYYY-MM-DD HH:mm',
      //     showTime: { format: 'HH:mm' },
      //   },
      // },
      {
        label: t(''),
        field: 'enterpriseId',
        fieldLabel: 'enterpriseName',
        component: 'Select',
        componentProps: () => {
          return {
            placeholder: t('请输入所属主体'),
            api: unitEnterpriseDominantListData,
            params: EnterpriseParams.value,
            fieldNames: { label: 'name', value: 'unitCode' },
            resultField: 'list',
            notFoundContent: '未查询到记录',
            filterTreeNode: false,
            allowClear: true,
            filterOption: false,
            immediate: true,
            onSearch: (value: string) => {
              EnterpriseParams.value.name = value;
              console.log(EnterpriseParams.value);
            },
          };
        },
      },
      {
        label: t('火情状态'),
        field: 'fireExecutionStatus',
        component: 'Select',
        componentProps: {
          placeholder: t('请选择火情状态'),
          options: [
            { label: t('火情已发生'), value: '已发生' },
            { label: t('火情已结束'), value: '已结束' },
          ]
        }
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('企业名称'),
      dataIndex: 'enterpriseName',
      key: 'a.enterprise_name',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('建筑名称'),
      dataIndex: 'buildName',
      key: 'a.build_name',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('区域名称'),
      dataIndex:'regionIname',
      key: 'a.region_iname',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('火情名称'),
      dataIndex: 'fireName',
      key: 'a.fire_name',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('详细地址'),
      dataIndex: 'location',
      key: 'a.location',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('火情确认时间'),
      dataIndex: 'fireConfirmTime',
      key: 'a.fire_confirm_time',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('确认用户姓名'),
      dataIndex: 'fireUserName',
      key: 'a.fire_user_name',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('火情状态'),
      dataIndex: 'fireExecutionStatus',
      key: 'a.fire_execution_status',
      sorter: true,
      width: 130,
      align: 'left',
      dictType: 'fire_status'
    },
    {
      title: t('操作'),
      dataIndex: 'action',
      key: 'action',
      width: 160,
      fixed: 'right',
      slot: 'action'
    }
  ];

  const [registerTable, { reload }] = useTable({
    api: fireDrillListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    actionColumn: null, // 移除原 actionColumn
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
  });

  const [registerDrawer, { openDrawer }] = useDrawer();

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleDelete(record: Recordable) {
    const params = { tid: record.tid };
    const res = await fireDrillDelete(params);
    showMessage(res.message);
    handleSuccess(record);
  }

  // async function handleEndFireDrill(record: Recordable) {
  //   Popconfirm.confirm({
  //     title: t('是否确认结束该火情演练'),
  //     onConfirm: async () => {
  //       try {
  //         const params = { FireInfoId: Number(record.id) }; // 将字符串类型的id转换为数字类型
  //         const res = await fireDrillEnded(params);
  //         showMessage(res.message);
  //         handleSuccess(record);
  //       } catch (error) {
  //         showMessage('结束火情演练失败');
  //       }
  //     },
  //   });
  // }

  function handleSuccess(record: Recordable) {
    reload({ record });
  }
</script>